<template>
	<view>
		<view class="warp" v-show="modalshow">
			<!-- 2023新版写法,sb腾讯 -->
			<!-- 	<view class="get_user">
		
			<! 下面是获取用户信息，两个获取用户数据的接口回收了，sb腾讯 -->
			<view class="warp-view">

				<button open-type="chooseAvatar" @chooseavatar="onChooseAvatar" style="padding-top: 20upx;">
					<image class="avatar" :src="avatarUrl" style="width: 100upx;height: 100upx"></image>
				</button>


				<view style="background: #fff;padding: 20upx;">
					<input type="nickname" class="weui-input" placeholder="请输入昵称" v-model="nickName"
						style="background: #f5f5f5;border: 1upx solid#ddd;border-radius: 8upx;padding: 8upx;" />
				</view>

				<view class="warp-text">{{massages}}</view>
				<view class="warp-flex">

					<button plain="true" @click="messcancel()">取消</button>
					<button plain="true" @click="getUserInfo">登录</button>
				</view>
			</view>
		</view>
		<!-- 及时反馈文件 ref相当于传统开发的组件id,就是表示不同 -->
		<HMmessages ref="HMmessages" @complete="HMmessages = $refs.HMmessages" @clickMessage="clickMessage"></HMmessages>
	</view>
</template>

<script>
	const defaultAvatarUrl =
		'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
	//引入公用用户存储数据
	import {
		login
	} from '../common/list.js'
	// 引入及时反馈组件
	import HMmessages from '@/components/HM-messages/HM-messages.vue'
	export default {
		name: 'modals',
		components: {
			HMmessages
		},
		data() {
			return {
				avatarUrl: defaultAvatarUrl,
				modalshow: false, //这是控制模态框隐藏和出现的
				massages: '',
				nickName: ""
			};
		},

		mounted() {

		},

		methods: {
			//这个是正确的写法
			onChooseAvatar(e) {
				console.log(e)
				const {
					avatarUrl
				} = e.detail
				this.avatarUrl = avatarUrl
			},
			//取消模态框
			messcancel() {
				this.modalshow = false
			},
			init(massage) {
				this.modalshow = true
				this.massages = massage
			},
			//发起登录取到用户信息
			async getUserInfo() { //sb腾讯
				if (this.avatarUrl != defaultAvatarUrl) {
					let result = await wx.cloud.uploadFile({
						cloudPath: new Date().getTime() + '.png',
						filePath: this.avatarUrl, // 文件路径
					})
					this.avatarUrl = result.fileID
				}

				if (!this.nickName) return this.massages = "请填写昵称"

				let thisUserRes = await wx.cloud.callFunction({
					name: 'login',
					data: {
						nickName: this.nickName,
						avatarUrl: this.avatarUrl,
					}
				})
				if (thisUserRes.result.code) {
					this.messcancel()
					this.HMmessages.show('登录成功', {
						icon: 'success',
						iconColor: '#ffffff',
						fontColor: '#ffffff',
						background: 'rgba(102, 0, 51, 0.6)'
					})
					this.$emit("loginSuccess", thisUserRes.result.data)
				}

			}


		}
	}
</script>

<style scoped>
	/* 这是模态框的弹窗布局 */
	.warp {
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.6);
		z-index: 9999;
	}

	.warp-view {
		width: 500upx;
		/* width: 100%; */
		/* height: 600upx; */
		background: #ffffff;
		margin: auto;
		position: absolute;
		-webkit-position: absolute;
		top: 20%;
		left: 0;
		right: 0;
		border-radius: 8upx;
		overflow: hidden;
	}

	.warp-text {
		text-align: center;
		margin-top: 30upx;
		font-size: 34upx;
		color: #666666;
	}

	.warp-flex {
		display: flex;
		justify-content: space-around;
		border-top: 1upx solid #eeeeee;
		height: 80upx;
		line-height: 80upx;
		-webkit-transform: translateZ(0);
	}

	.warp-flex button {
		border: none;
		font-size: 30upx;
	}

	.change {
		border: none;
		font-size: 30upx;
	}

	.get_user {
		margin-top: 100upx;
		margin-bottom: 80upx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.get_user image {
		width: 100upx;
		height: 100upx;
		border-radius: 20%;
	}

	.get_user input {
		margin-top: 10upx;
		border: 1rpx solid grey;
		border-radius: 10upx;
	}
</style>